package com.example.jetpacker.livedata

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel

/**
 * map
 * switchMap
 */
class DataTransitionViewModel : ViewModel() {

    private val userLiveData = MutableLiveData<User>()

    val userNameLiveData: LiveData<String> = Transformations.map(userLiveData) {
        "${it.firstName} ${it.firstName}"
    }

    fun getUser(userId: Int): LiveData<User> {
        return Repository.getUser(userId)
    }


    private val userIdLiveData=MutableLiveData<Int>()

    val user=Transformations.switchMap(userIdLiveData){
        Repository.getUser(it)
    }

    fun getUser2(userId: Int) {
        userIdLiveData.value=userId
    }


    private val refreshLiveData = MutableLiveData<Any>()

    fun refresh() {
        refreshLiveData.value=refreshLiveData.value
    }

}